<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>过滤 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_filtering_queries.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_filtering_queries.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_filtering_queries.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_filtering_queries.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="_filtering_queries_and_aggregations.html">过滤和聚合</a></span>
»
<span class="breadcrumb-node">过滤</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_filtering_queries_and_aggregations.html">« 过滤和聚合</a>
</span>
<span class="next">
<a href="_filter_bucket.html">过滤桶 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_filtering_queries"></a>过滤查询 (Filtering Queries)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/300_Aggregations/45_filtering.asciidoc">edit</a>
</h2>
</div></div></div>
<p>如果我们想找到售价在 $10,000 美元之上的所有汽车同时也为这些车计算平均售价，
 可以简单地使用一个 <code class="literal">constant_score</code> 查询和 <code class="literal">filter</code> 约束：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /cars/transactions/_search
{
    "size" : 0,
    "query" : {
        "constant_score": {
            "filter": {
                "range": {
                    "price": {
                        "gte": 10000
                    }
                }
            }
        }
    },
    "aggs" : {
        "single_avg_price": {
            "avg" : { "field" : "price" }
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/300_Aggregations/45_filtering.json"></div>

<p style="color: #666;">
&gt; 译者注: <code class="literal">constant_score</code>是一个固定评分的查询, 就是不再计算每个文档的评分, 属于一种 non-scoring查询.
</p>
<p>这正如我们在前面章节中讨论过的那样，从根本上讲，使用 不计算文档得分(non-scoring) 的查询和使用 <code class="literal">match</code> 查询没有任何区别。查询（包括一个过滤器 (filter) ）返回一组文档的子集，聚合正是操作这些文档。恰好它忽略了评分，并有可能会主动缓存结果数据等等。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="_filtering_queries_and_aggregations.html">« 过滤和聚合</a>
</span>
<span class="next">
<a href="_filter_bucket.html">过滤桶 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>